package cn.lyjuan.android.mylibrary.util;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.LocationManager;
import android.os.Build;
import android.os.Process;
import android.provider.Settings;
import android.support.v4.content.PermissionChecker;
import android.text.TextUtils;
import android.util.Log;

import cn.lyjuan.android.mylibrary.context.BaseApplication;

/**
 * 定位权限工具
 * 参考：https://www.cnblogs.com/wlrhnh/p/7389930.html
 * Created by chad on 14/11/2017.
 */
public class LocationUtils
{
    private static final String TAG = LocationUtils.class.getSimpleName();

    /**
     * 应用是否可定位，包含系统是否开启定位功能及应用是否授予定位权限
     *
     * @return
     */
    public static boolean canLocation()
    {
        return isLocationEnabled() && isLocationGranted();
    }

    /**
     * 是否授予应用定位权限
     *
     * @return
     */
    public static boolean isLocationGranted()
    {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
        {
            boolean find = BaseApplication.getContext().checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED;

            boolean coarse = BaseApplication.getContext().checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED;

            return find && coarse;
        } else
        {
            boolean find = PermissionChecker.checkPermission(BaseApplication.getContext(),
                    Manifest.permission.ACCESS_FINE_LOCATION, Process.myPid(), Process.myUid(),
                    BaseApplication.getContext().getPackageName()) == PackageManager.PERMISSION_GRANTED;

            boolean coarse = PermissionChecker.checkPermission(BaseApplication.getContext(),
                    Manifest.permission.ACCESS_COARSE_LOCATION, Process.myPid(), Process.myUid(),
                    BaseApplication.getContext().getPackageName()) == PackageManager.PERMISSION_GRANTED;

            return find && coarse;
        }
    }

    /**
     * 系统是否开启定位服务
     *
     * @return
     */
    public static boolean isLocationEnabled()
    {
        LocationManager locationManager
                = (LocationManager) BaseApplication.getContext().getSystemService(Context.LOCATION_SERVICE);
        // 通过GPS卫星定位，定位级别可以精确到街（通过24颗卫星定位，在室外和空旷的地方定位准确、速度快）
        boolean gps = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
        // 通过WLAN或移动网络(3G/2G)确定的位置（也称作AGPS，辅助GPS定位。主要用于在室内或遮盖物（建筑群或茂密的深林等）密集的地方定位）
        boolean network = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
        if (gps || network)
        {
            return true;
        }

        int locationMode = 0;
        String locationProviders;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
        {
            try
            {
                locationMode = Settings.Secure.getInt(BaseApplication.getContext().getContentResolver(), Settings.Secure.LOCATION_MODE);
            } catch (Settings.SettingNotFoundException e)
            {
                Log.d(TAG, "setting not found", e);
                return false;
            }
            return locationMode != Settings.Secure.LOCATION_MODE_OFF;
        } else
        {
            locationProviders = Settings.Secure.getString(BaseApplication.getContext().getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);


            return !TextUtils.isEmpty(locationProviders);
        }
    }
}
